package com.longrise.android.byjk.plugins.tabfirst.revision;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.plugins.tabfirst.revision.adapter.HomeFragAdapter;
import com.longrise.android.byjk.utils.Tracker_Google;

/**
 * Created by sujizhong on 2017/2/21.
 * 资讯
 */
public class ConsultationActivity extends BaseActivity {

    private static final String TAG = "ConsultationActivity";

    private final String SAVE_KEY_CURRENT_PAGE = "SAVE_KEY_CURRENT_PAGE";

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private HomeFragAdapter mAdapter;


    private int mCurrentPage = 0;
    private Toolbar mToolbar;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_consultation;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle(getString(R.string.zixun));
        mToolbar.setTitleTextColor(Color.parseColor("#4D4D4D"));
        setSupportActionBar(mToolbar);
        mViewPager = (ViewPager) findViewById(R.id.container);
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        adapter();

        onEvent();

        Tracker_Google.getInstance().addGoogleScreenName("保易健康头条");
    }


    private void onEvent() {

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void adapter() {
        mAdapter = new HomeFragAdapter(getSupportFragmentManager(), HomeFragAdapter.mInsFrag);
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout){
            @Override
            public void onPageSelected(int position) {
                addEvent(position);
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.setCurrentItem(mCurrentPage);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mViewPager != null) {
            outState.putInt(SAVE_KEY_CURRENT_PAGE, mViewPager.getCurrentItem());
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null && mViewPager != null) {
            mCurrentPage = savedInstanceState.getInt(SAVE_KEY_CURRENT_PAGE);
            mViewPager.setCurrentItem(mCurrentPage);
        }
    }

    private void addEvent(int position) {
        if(position == 0){
            Tracker_Google.getInstance().addEvent("QA", "Insurance-headlines", "保险头条");
        }else{
            Tracker_Google.getInstance().addEvent("QA", "Health", "健康养生");
        }
    }

    private void release() {
        mViewPager = null;
        mTabLayout = null;
        mAdapter = null;
    }

    private void close() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        release();
    }
}
